package com.yoooya.base.third.utils.http;

import com.yoooya.base.third.common.error.WxError;
import org.apache.http.Consts;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;

/**
 * 简单的POST请求执行器，请求的参数是String, 返回的结果也是String
 * 
 * 
 * @创建人 Sun Yijing
 * @创建时间 2018-07-08
 */
public class SimplePostRequestExecutor implements
		RequestExecutor<String, String> {

	@Override
	public String execute(CloseableHttpClient httpclient, String uri,
                          String postEntity) throws ClientProtocolException, IOException {
		HttpPost httpPost = new HttpPost(uri);

		if (postEntity != null) {
			StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
			httpPost.setEntity(entity);
		}

		try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
			String responseContent = Utf8ResponseHandler.INSTANCE
					.handleResponse(response);
			WxError error = WxError.fromJson(responseContent);
			if (error.getErrorCode() != 0) {
			}
			return responseContent;
		}
	}

}
